<?php
/**
*
* @package phpBB3
* @version $Id: functions.php 8491 2008-04-04 11:41:58Z acydburn $
* @copyright (c) 2005 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

function generate_cryptlink($message, $post_data)
{
    global $db, $user;
    if(preg_match_all('~<a(| class="[^"]*") href="([a-z][a-z\d+\-.]*:/{2}[^"]*)"(| class="[^"]*")>([^<]*)</a>~U',$message, $return))
    {
        for($i=0; $i < count($return['0']); $i++)
        {
            $url = $return['2'][$i];
            $txt =  $return['4'][$i];
            $sql = "SELECT id FROM " . LINK_TABLE . "

                WHERE forum_id = '" . $post_data['forum_id'] . "'
                AND post_id = '" . $post_data['post_id'] .  "'
                AND url = '" . $url . "'";
            $result = $db->sql_query_limit($sql, 1);
            if($db->sql_affectedrows($result) == 1 )
            {
                $row = $db->sql_fetchrow($result);
                $replace = $row['id'];
            }
            else
            {
                $random_id = substr(md5(rand(0,100) . mircotime),3, 6);
                $sql = "INSERT INTO " .  LINK_TABLE . "
                    (id, url, forum_id, post_id)
                    VALUES ('" . $random_id . "','" .  $url . "','" .  $post_data['forum_id'] . "','" .  $post_data['post_id'] . "')";
                $db->sql_query($sql);
                $replace = $random_id;
            }
            $message = str_replace('href="' . $url, 'href="' . append_sid('./link.php', 'link=' . $replace), $message);
            $message = str_replace($url, $user->lang['LINK_REPLACE'] . $replace, $message);
        }

    }
    return $message;

}
?>